package com.fy56.platform.logistics.restful.api.controller.wx;

import com.alibaba.fastjson.JSON;
import com.fy56.platform.logistics.common.ResultModel;
import com.fy56.platform.logistics.common.ResultStatus;
import com.fy56.platform.logistics.model.Advertise;
import com.fy56.platform.logistics.model.Company;
import com.fy56.platform.logistics.restful.api.app.authorization.annotation.Authorization;
import com.fy56.platform.logistics.service.web.IAdvertiseService;
import com.fy56.platform.logistics.service.wx.ICompanyInfoService;
import com.google.common.collect.Lists;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 广告信息RestController
 * Created by Ning on 2018/5/14.
 */
@Api(value = "Advertise", description = "广告信息", produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/advertise")
public class AdvertiseController {

    @Autowired
    private IAdvertiseService iAdvertiseService;

    @Autowired
    private ICompanyInfoService iCompanyService;

    private static Logger log = Logger.getLogger(AdvertiseController.class);

    @RequestMapping(value = "/findAdvertiseList", method = RequestMethod.POST)
    @ApiOperation(value = "获取广告信息")
    @Authorization
    @ResponseBody
    public ResponseEntity findAdvertiseList(@RequestParam String data) {
        Long begin = System.currentTimeMillis();//开始时间
        List<Advertise> list = Lists.newArrayList();
        Map<String,Object> parameter = null;
        try{
            parameter = JSON.parseObject(data);
        }catch(Exception e){
            log.error("参数转换错误：",e);
            return new ResponseEntity(new ResultModel(100,"参数有误",null), HttpStatus.OK);
        }
        Map<String ,Object> par = new HashMap<String,Object>();
        par.put("type",0);
        par.put("code","");
        if(parameter != null && !parameter.isEmpty()){
            Object codeObj = parameter.get("code");
            if(null != codeObj){
                par.put("code",codeObj.toString());
                Company c = iCompanyService.getCompanyByCode(codeObj.toString());
                if(null != c){
                    par.put("limit",c.getImgLimit() == null ? 4 : c.getImgLimit());
                }
            }
        }

        try {
            list = iAdvertiseService.findHomePageAdvertiseList(par);
            Long end = System.currentTimeMillis();//结束时间
            log.info("findAdvertiseList总用时：" + (end - begin) + "毫秒。");
            return new ResponseEntity(ResultModel.ok(list), HttpStatus.OK);
        } catch (Exception e) {
            log.error("获取广告信息失败：" + data,e);
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR, "获取广告信息失败"), HttpStatus.OK);
        } finally {
            list = null;
        }
    }

}
